package com.zimi.controller;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("qr")
@Api(value = "QRApi", tags = {"二维码处理接口"})
@RestController
public class QRApi {

    @ApiOperation("生成二维码")
    @RequestMapping(value = "writ", method = {RequestMethod.GET})
    public String writ(HttpServletRequest req, String str) {
        System.out.println(1234);
        final int width = 300;
        final int height = 300;
        final String format = "png";
        final String content = str;
        //定义二维码的参数
        HashMap hints = new HashMap<EncodeHintType, Comparable<String>>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        //生成二维码
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path file = new File(req.getSession().getServletContext().getRealPath("") + "static\\img.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return "done";
    }

    @ApiOperation("读取二维码")
    @RequestMapping(value = "read", method = {RequestMethod.POST})
    public String read(@RequestParam("file") MultipartFile file) throws Exception {
        MultiFormatReader formatReader = new MultiFormatReader();

        //File file = new File(req.getSession().getServletContext().getRealPath("") + "static\\img.jpg");

        BufferedImage image = ImageIO.read(file.getInputStream());
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

        //定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

        Result result = formatReader.decode(binaryBitmap, hints);

        System.out.println("二维码解析结果：" + result.toString());
        System.out.println("二维码的格式：" + result.getBarcodeFormat());
        System.out.println("二维码的文本内容：" + result.getText());

        Map<String, String> map = new HashMap<String, String>();
        return result.getText();
    }
}
